package top.callname.dao;

import java.sql.SQLException;
import java.util.ArrayList;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.log4j.Logger;

import top.callname.bean.ConstanstBean;
import top.callname.bean.SignBean;
import top.callname.bean.SignPageList;

public class SignDao extends BaseDao{

	
	private Logger logger = Logger.getLogger(SignDao.class);
	
	/**
	 * 修改签到任务表
	 * @param signId
	 * @return boolean
	 */
	public boolean updateStatusBySignId(Integer signId ,Integer pid){
		String sql =  "update t_sign set sign_status=0 where sign_id="+signId+" and pid="+pid ;
		logger.info("修改签到任务表："+sql);
		return adu(sql);
	}
	
	/**
	 * 查询当前登录人员创建的有效任务
	 * @param signId
	 * @return SignBean
	 */
	public SignBean selectByPid(Integer pid){
		SignBean bean = null;
		String sql =  "select  sign_id,pid,cids,rand_no,time_out,sign_desc,sign_status,createtime "
				+ "  from t_sign where  sign_status=1 and pid="+pid + " limit 0,1";
		logger.info("根据pid查询记录信息："+sql);
		res = query(sql);
		try {
			if(res.next()){
				bean = new SignBean();
				bean.setSignId(res.getInt("sign_id"));
				bean.setCids(res.getString("cids"));
				bean.setDesc(res.getString("sign_desc"));
				bean.setRandNo(res.getString("rand_no"));
				bean.setTimeOut(res.getInt("time_out"));
				bean.setStatus(res.getInt("sign_status"));
				bean.setStatusName(ConstanstBean.SING_STATUS_MAP.get(bean.getStatus()));
				String date = res.getString("createtime");
				bean.setCreatetime(date.substring(0,date.length()-2));
				return bean;
			}
			closeDB();
		} catch (SQLException e) {
			logger.error("查询 t_sign 出错："+e);
		}
		return null;
	}
	
	public SignBean selectBySignId(Integer signId){
		SignBean bean =null;
		String sql =  "select  sign_id,pid,cids,rand_no,time_out,sign_desc,sign_status,createtime "
				+ "  from t_sign where sign_id="+signId;
		logger.info("根据sign_id查询记录信息："+sql);
		res = query(sql);
		try {
			if(res.next()){
				bean = new SignBean();
				bean.setSignId(res.getInt("sign_id"));
				bean.setCids(res.getString("cids"));
				bean.setDesc(res.getString("sign_desc"));
				bean.setRandNo(res.getString("rand_no"));
				bean.setTimeOut(res.getInt("time_out"));
				bean.setStatus(res.getInt("sign_status"));
				bean.setStatusName(ConstanstBean.SING_STATUS_MAP.get(bean.getStatus()));
				String date = res.getString("createtime");
				bean.setCreatetime(date.substring(0,date.length()-2));
			}
			closeDB();
		} catch (SQLException e) {
			logger.error("查询 t_sign 出错："+e);
		}
		return bean;
	}
	
	public SignBean selectByRandNo(String randNo){
		SignBean bean = null;
		String sql =  "select  sign_id,pid,cids,rand_no,time_out,sign_desc,sign_status,createtime"
				+ "  from t_sign where rand_no='"+randNo+"' and sign_status=1  limit 0,1 ";
		logger.info("根据校验码查询记录信息："+sql);
		res = query(sql);
		try {
			if(res.next()){
				bean = new SignBean();
				bean.setSignId(res.getInt("sign_id"));
				bean.setCids(res.getString("cids"));
				bean.setDesc(res.getString("sign_desc"));
				bean.setRandNo(res.getString("rand_no"));
				bean.setTimeOut(res.getInt("time_out"));
				bean.setStatus(res.getInt("sign_status"));
				bean.setStatusName(ConstanstBean.SING_STATUS_MAP.get(bean.getStatus()));
				String date = res.getString("createtime");
				bean.setCreatetime(date.substring(0,date.length()-2));
			}
			closeDB();
		} catch (SQLException e) {
			logger.error("查询 t_sign 出错："+e);
		}
		return bean;
	}
	
	/**
	 * 新增记录
	 * @param stuNo
	 * @param openid
	 * @return boolean
	 */
	public boolean addSign(SignBean signInfo){
		String sql = "insert into t_sign (pid,cids,time_out,sign_desc,rand_no,createtime) "
				+ "values ( '"
						+signInfo.getPid()+"','"
						+signInfo.getCids()+"','"
						+signInfo.getTimeOut()+"','"
						+signInfo.getDesc()+"','"
						+signInfo.getRandNo()+"',NOW() )";
		logger.info("新增签到记录表："+sql);
		return adu(sql);
	}
	
	/**
	 * 
	 * Description:根据登录人pid分页查询
	 * @param pid
	 * @return List<ClassBean>
	 */
	public SignPageList selectPageByPid(int pageNow , Integer pid){
		SignPageList pageList = new SignPageList();
		pageList.setPageNow(pageNow);
		//查询总记录
		int totalSize = getCount("t_sign"," where pid="+pid);
		pageList.setTotalSize(totalSize);
		ArrayList<SignBean> list = new ArrayList<SignBean>();
		
		String limit = "order by sign_id desc limit "+pageList.getStart()+","+pageList.getOffset();
		String sql =  "select  sign_id,pid,cids,rand_no,time_out,sign_desc,sign_status,createtime"
				+ "  from t_sign where pid="+pid+" "+limit;
		logger.info("分页查询签到记录信息："+sql);
		res = query(StringEscapeUtils.escapeSql(sql));
		try {
			while(res.next()){
				SignBean bean = new SignBean();
				bean.setSignId(res.getInt("sign_id"));
				bean.setCids(res.getString("cids"));
				bean.setDesc(res.getString("sign_desc"));
				bean.setRandNo(res.getString("rand_no"));
				bean.setTimeOut(res.getInt("time_out"));
				bean.setStatus(res.getInt("sign_status"));
				bean.setStatusName(ConstanstBean.SING_STATUS_MAP.get(bean.getStatus()));
				String date = res.getString("createtime");
				bean.setCreatetime(date.substring(0,date.length()-2));
				list.add(bean);
			}
			closeDB();
		} catch (SQLException e) {
			logger.error("查询 t_sign 出错："+e);
		}
		pageList.setList(list);
		return pageList;
	}
	
	
	/**
	 * 
	 * Description:分页查询全部
	 * @param pid
	 * @return List<ClassBean>
	 */
	public SignPageList selectPage(int pageNow){
		SignPageList pageList = new SignPageList();
		pageList.setPageNow(pageNow);
		//查询总记录
		int totalSize = getCount("t_sign");
		pageList.setTotalSize(totalSize);
		
		ArrayList<SignBean> list = new ArrayList<SignBean>();
		String limit = "order by sign_id desc limit "+pageList.getStart()+","+pageList.getOffset();
		String sql =  "select  sign_id,pid,cids,rand_no,time_out,sign_desc,sign_status,createtime"
				+ "  from t_sign "+limit;
		logger.info("分页查询签到记录信息："+sql);
		res = query(StringEscapeUtils.escapeSql(sql));
		try {
			while(res.next()){
				SignBean bean = new SignBean();
				bean.setSignId(res.getInt("sign_id"));
				bean.setCids(res.getString("cids"));
				bean.setDesc(res.getString("sign_desc"));
				bean.setRandNo(res.getString("rand_no"));
				bean.setTimeOut(res.getInt("time_out"));
				bean.setStatus(res.getInt("sign_status"));
				bean.setStatusName(ConstanstBean.SING_STATUS_MAP.get(bean.getStatus()));
				String date = res.getString("createtime");
				bean.setCreatetime(date.substring(0,date.length()-2));
				list.add(bean);
			}
			closeDB();
		} catch (SQLException e) {
			logger.error("查询 t_sign 出错："+e);
		}
		pageList.setList(list);
		return pageList;
	}
	
}
